package com.chenyue.cm.match.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.match.dao.JdMatchItemRelationMapper;
import com.chenyue.cm.match.domain.JdMatchItem;
import com.chenyue.cm.match.domain.JdMatchItemExample;
import com.chenyue.cm.match.domain.JdMatchItemRelation;
import com.chenyue.cm.match.service.JdMatchItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author chenyue-cm
 * @date 2021-07-27
 */
@RestController
@RequestMapping("/jdMatchItem")
public class JdMatchItemController {

    private final JdMatchItemService jdMatchItemService;
    public JdMatchItemController(JdMatchItemService jdMatchItemService) {
        this.jdMatchItemService = jdMatchItemService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdMatchItem> result = jdMatchItemService.getPageBean(pageInfo, JdMatchItem.class);
        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdMatchItem vo) {
        boolean success = jdMatchItemService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(JdMatchItem vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = jdMatchItemService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdMatchItemExample aExample = new JdMatchItemExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdMatchItemService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdMatchItemService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
    }

    @Log
    @RequestMapping("/getListByTypeIdPage")
    public Success getListByTypeIdPage(HttpServletRequest request) {
        String typeId = request.getParameter("typeId");
        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        LambdaQueryWrapper<JdMatchItem> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(typeId)) {
            wrapper.eq(JdMatchItem::getTypeId, typeId);
        }
        List<JdMatchItem> list = jdMatchItemService.list(wrapper);
        //封装返回结果
        PageInfo<JdMatchItem> result = new PageInfo<JdMatchItem>(list);

        return Success.success(result);
    }

    @Log
    @RequestMapping("/getListByTypeId")
    public Success getListByTypeId(HttpServletRequest request) {
        String typeId = request.getParameter("typeId");
        LambdaQueryWrapper<JdMatchItem> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(typeId)) {
            wrapper.eq(JdMatchItem::getTypeId, typeId);
        }
        List<JdMatchItem> list = jdMatchItemService.list(wrapper);

        return Success.success(list);
    }


    @Log
    @RequestMapping("/getMatchItemList")
    public Success getMatchItemList(HttpServletRequest request) {
        String matchId = request.getParameter("matchId");
        List<JdMatchItemRelation> list = jdMatchItemService.getMatchItemList(Integer.parseInt(matchId));
        return Success.success(list);
    }

}
